package com.bdqn;

import com.bdqn.entity.User;
import com.bdqn.repository.UserRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class UserRepositoryTester {
    
    @Resource
    private UserRepository userRepository;

    @Test
    public void testInsert() {//测试新增
        User user = new User("JPA", "123456", 2L, 1);
        userRepository.save(user);
    }

    @Test
    public void testGet() {//测试按主键查询
        //User user = userRepository.getOne(2L);//不是使用该方法
        User user = userRepository.findById(2L).get();
        System.out.println("usrName：" + user.getUsrName());
        }

    @Test
    public void testDelete() {
        User user = userRepository.findById(20L).get();
        // 删除对象（根据属性中的ID进行定位）
        userRepository.delete(user);
        System.out.println("删除成功！");
    }

    @Test
    public void testFindPageByUsrRoleId() {
        int page = 0, size = 2;//分页要素
        //控制分页数据的排序，可以选择升序和降序
        Sort sort = Sort.by(Sort.Direction.DESC, "usrId");
        //控制分页的辅助类，可以设置页码、每页的数据条数、排序等
        Pageable pageable = PageRequest.of(page, size, sort);
        Page<User> userPager = userRepository.findPageByUsrRoleId(8l,pageable);
        System.out.println("总记录数 --------> " + userPager.getTotalElements());
        System.out.println("总页数 ----------> " + userPager.getTotalPages());
        System.out.println("当前页数 --------> " + userPager.getNumber());
        System.out.println("每页记录数 ------> " + userPager.getSize());
        System.out.println("当前记录： ------> "+userPager.getNumberOfElements());
        for(User user:userPager.getContent()){//获得查询记录
            System.out.println("usrId："+user.getUsrId());
        }
    }
    @Test
    public void testFindByUsrNameLike() {
    List<User> list = userRepository.findByUsrNameLike("%s%");//查询参数必须带% 号
        if(list!=null){
        for(User user:list){
            System.out.println("usrName："+user.getUsrName());
        }
    }
}
    @Test
    public void testFindByUsrName() {
        List<User> list = userRepository.findByUsrName("小李老师");
        for (User user : list) {
            System.out.println(user.toString());
        }
    }
    @Test
    public void testModifyNameById() {
        int flag = userRepository.modifyNameById("小李老师", 22L);
        if (flag == 1) {
            System.out.println("修改成功！");
        } else {
            System.out.println("修改失败！");
        }
    }
    @Test
    public void testFindByUsrNameAndUsrPassword() {
        List<User> list = userRepository.findByUsrNameAndUsrPassword("小李老师", "123456");
        for (User user : list) {
            System.out.println(user.toString());
        }
    }
    @Test
    public void testFindByRoleIdNative() {
        List<User> list = userRepository.findByRoleIdNative(1L);
        for (User user : list) {
            System.out.println(user.toString());
        }
    }
    @Test
    public void testFindByRoleIdParam() {  // 使用@Param注解设置参数
        List<User> list = userRepository.findByRoleIdParam(2L);
        for (User user : list) {
            System.out.println(user.toString());
        }
    }
    }


